import React, {useEffect, useState} from "react";
import styled from "@emotion/styled";
import Logo from './storage.png';
import {Post} from "../../utils/http";
import {message} from "antd";
import {useNavigate} from "react-router-dom";
import {useDispatch} from "react-redux";
import {changeLoginStatusActionCreator} from "../../redux/user/action";
import {UserState} from "../../redux/user/reducer";

interface FormProp {
    username: string;
    password: string;
}

type UserInfo = Omit<UserState, "status">

export const LoginPage = () => {

    let navigate = useNavigate();

    const dispatch = useDispatch();

    useEffect(() => {
        document.title = '聚合云盘：登录系统'
    }, [])

    const [dataForm, setDataForm] = useState<FormProp>({username: "", password: ""})

    const loginUser = () => {
        Post<UserInfo>('/install/login', dataForm).then(res => {
            if (res.code === '100200') {
                message.success(res.msg)
                dispatch(changeLoginStatusActionCreator({status: true, ...res.data}))
                localStorage.setItem("token", res.data.token)
                navigate('/source')
            } else {
                message.error(res.data)
            }
        })
    }

    return <Container>
        <div className="login-area">
            <div className="left-container">
                <div className="form">
                    <h2>欢 迎 登 录</h2>
                    <input type="text" name="username" placeholder="账号" onChange={e => setDataForm({...dataForm, username: e.target.value})} />
                    <input type="password" name="password" placeholder="密码" onChange={e => setDataForm({...dataForm, password: e.target.value})} />
                    <a href="#" className="forget-password">忘记密码</a>
                    <button className="signIn" onClick={loginUser}>sign in</button>
                </div>
            </div>
            <div className="right-container">
                <div className="overlay">
                    <div className="overlay-panel">
                        <img src={Logo} alt="logo"/>
                    </div>
                </div>
            </div>
        </div>
    </Container>

}


const Container = styled.div`
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f6f5f7;
  .login-area {
    position: relative;
    width: 768px;
    height: 480px;
    background-color: white;
    box-shadow: 0 14px 28px rgb(0 0 0 / 25%), 0 10px 10px rgb(0 0 0 / 20%);
    border-radius: 10px;
    overflow: hidden;
    .left-container {
      position: absolute;
      top: 0;
      width: 50%;
      height: 100%;
      background-color: white;
      transition: all 0.6s ease-in-out;
      .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 100%;
        width: 100%;
        padding: 0 50px;
        input {
          width: 100%;
          margin: 8px 0;
          padding: 12px;
          background-color: #eee;
          border: none;
        }
        .forget-password {
          display: inline-block;
          height: 20px;
          text-decoration: none;
          color: #bbb;
          text-transform: capitalize;
          font-size: 12px;
        }

        .forget-password:hover {
          color: lightslategray;
          border-bottom: 2px solid #ff4b2b;
        }
        button {
          background: #ff4b2b;
          padding: 10px 50px;
          border: 1px solid transparent;
          border-radius: 20px;
          text-transform: uppercase;
          color: white;
          margin-top: 10px;
          outline: none;
        }

        button:active {
          transform: scale(0.95);
        }
      }
    }
    .right-container {
      position: absolute;
      top: 0;
      width: 50%;
      height: 100%;
      z-index: 100;
      right: 0;
      overflow: hidden;
      transition: all 0.6s ease-in-out;
      .overlay {
        position: absolute;
        width: 200%;
        height: 100%;
        left: -100%;
        background-color: #ff4b2b;
        .overlay-panel {
          position: absolute;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          width: 50%;
          height: 100%;
          color: white;
          padding: 0 40px;
          text-align: center;
          right: 0;
        }
      }
    }
  }
`
